home *** CD-ROM | disk | FTP | other *** search
- Path: ix.netcom.com!news
- From: miker3@ix.netcom.com (Mike Rubenstein)
- Newsgroups: comp.lang.c++
- Subject: Re: extern consts
- Date: Sun, 17 Mar 1996 14:26:40 GMT
- Organization: Netcom
- Message-ID: <314c2077.138956468@nntp.ix.netcom.com>
- References: <4idbcv$ue2@news7.erols.com>
- NNTP-Posting-Host: ix-dc12-26.ix.netcom.com
- X-NETCOM-Date: Sun Mar 17 8:29:31 AM CST 1996
- X-Newsreader: Forte Agent .99d/32.182
-
- ccobb <ccobb@erols.com> wrote:
-
- > The following code works on one C++ compiler but does not work on two
- > others I have tried. Can anyone give me an authoritive answer as to
- > whether this is legal C++?
- >
- > --- xxx.h ---
- > extern const int MYCONST;
- > --- xxx.cc ---
- > #include "xxx.h"
- > const int MYCONST = 11;
- > --- yyy.cc ---
- > #include <iostream.h>
- > #include "xxx.h"
- > main()
- > {
- > char myarray[MYCONST];
- >
- > cout << "MYCONST is " << MYCONST << endl;
- > cout << "sizeof myarray[] is " << sizeof(myarray) << endl;
- > return 0;
- > }
- > --- end ---
- >
- > The whole point here is whether consts can be extern. Arrays require
- > constant expressions. Techincally, the array size is a constant because
- > it is declared const. However, the value of the constant is set in
- > another file. This means that you could change your constants without
- > having to recompile! If you think the above is trivial you are missing
- > the point. The above is not possible in C nor in two of the three C++
- > compilers I have tried it on.
- >
- > Any comments?
-
- It's not legal. extern const is legal, but in any compilation unit
- that does not see the initializer the const variable is not a constant
- expression.
-
- Michael M Rubenstein
-